/**
 * <p></p>
 * xtype = cceafbrowsepanel

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.BrowsePanel
 * @extends Ext.container.Container
 * @since 1.0
 */
Ext.define('cceaf.control.BrowsePanel', {
    extend: 'Ext.container.Container',
    alias: 'widget.cceafbrowsepanel',
    requires: [
        'Ext.tree.Panel'
    ],
    
    /**
	 * 树的model
	 *
     * @property model 
     * @type String
     * @default "cceaf.model.main.TreeNodeModel"
     */
	model: "cceaf.model.main.TreeNodeModel",
	
	/**
	 * 根节点名称
	 *
     * @property rootName 
     * @type String
     * @default "根节点"
     */
	rootName: "根节点",
	
	/**
	 * 单据类型
	 *
     * @property billType 
     * @type number
     * @default 1 标签
     */
	billType: 1,
	
    initComponent : function(){
		var config = this;
		
		var model = config.model;
		if (!model) {
			cceaf.util.ExtHelper.showErrorDialog("请设置树的model！");
			return;
		}
		if(!Ext.ModelManager.getModel(model)) {
    		alert(config.getXType() + "的" + model + '不存在！');
    		return;
    	}
		
		//定义数据源
		var ds = new Ext.data.TreeStore({
	    	model: model,
	    	autoLoad: true,
	        proxy: {
	            type: 'ajax',
	            url: '/' + cceaf.Global.WEB_APP_NAME + '/sysmanager/browseConfig!findTreeNodeList.action',
	            extraParams: {
	            	billType: config.billType
	            }
	        },
	        sorters: [{
	           	sorterFn:function(data1,data2){
					var num1 = Ext.Number.from(data1.data.id,0);
					var num2 = Ext.Number.from(data2.data.id,0);
					if(num1<num2){
						return -1;
					}else if(num1==num2){
						return 0;
					}else{
						return 1;
					}
				}
	        }, {
	            property: 'text',
	            direction: 'ASC'
	        }],
	        
	        listeners: {
	        	'load': function(){
	        		var jr = ds.getProxy().getReader();
	        		if(!jr.jsonData) {
	        			cceaf.util.ExtHelper.showErrorDialog("没有设置正确的action或没有返回正确的json字符串！");
	        			return;
	        		}
	        		
	        		var message = jr.jsonData.message;
	        		var url = jr.jsonData.url;
	        		cceaf.util.ExtHelper.showInfoDialog(message,url);
	        	},
	        	'loadexception': function(){
	        		var jr = ds.getProxy().getReader();
	        		if(jr.jsonData){
		        		var message = jr.jsonData.message;
		        		var url = jr.jsonData.url;
		        		cceaf.util.ExtHelper.showErrorDialog(message,url);
	        		}
	        		else{
	        			cceaf.util.ExtHelper.showErrorDialog("返回无效的JSONDATA数据");
	        		}
	        	}
	        }
	    });
	    
		Ext.applyIf(this, {
			layout: {
		        type: 'hbox',
		        align: 'stretch'
		    },
	        items: [{
	            itemId: 'nodesArea',
	            xtype: 'treepanel',
	            store: ds,
		        frame: true,
		        autoScroll:true,
				animate:true,
				enableDD:true,
				containerScroll: true,
		        anchor: '100%',
	            
	            flex: 1,
	            minWidth: 200,
	        	root: {
	                id: '',
	                text: config.rootName,
	                expanded: true
	            },
	            rootVisible: true
	        },
	        {
	            itemId: 'viewArea',
	            items: [],
	            flex: 5
	        }]
		});
		
		cceaf.control.BrowsePanel.superclass.initComponent.call(this);
    },
    
    initEvents: function() {
    	cceaf.control.BrowsePanel.superclass.initEvents.call(this);
    	
    	var treepanel = this.down('#nodesArea');
    	treepanel.on('itemclick',this.treeItemClick);
    },
    
    /**
	 * 单击节点
	 */
    treeItemClick: function(view, record){
    	var nodesArea = this;
    	var viewArea = nodesArea.up().down('#viewArea'); 
    	
    	var id = record.get('id');
    	var parameter = record.get('parameter');
    	var parameterJson = Ext.JSON.decode(parameter);
    	parameterJson.itemId = id;
    	parameterJson.height = cceaf.util.ExtHelper.getMainContentHeight()-52;
    	
    	var item = viewArea.down('#' + id);
    	if (!item) {
    		var temp = viewArea.getComponent(0);
    		if (temp) {
    			viewArea.remove(temp);
    		}
    		viewArea.add(parameterJson);
    	}
    }
    
    
});